package com.tedu.cotroller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;

/**
 * @author CCFu
 * @version 1.0
 * @date 2021/12/15 11:51
 */
@WebServlet("/user/addServlet")
public class addServlet extends HttpServlet {
    //表单提交 提交方式为post才会用这个方式
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String name = request.getParameter("name");
        String age = request.getParameter("age");
        System.out.println(name);
        System.out.println(age);
        PreparedStatement ps = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//5版本的jar包位置
            String url = "jdbc:mysql://localhost:3306/cgb2021?characterEncoding = utf8";
            Connection c = DriverManager.getConnection(url, "root", "root");

            String sql = "insert into dept values(null,?,?)";
            ps = c.prepareStatement(sql);
//        ps.setObject(1,deptno1);
            ps.setObject(1,name);
            ps.setObject(2,age);
            ps.executeUpdate();

        } catch (Exception e) {
            System.out.println("失败！");
        }finally {
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
        //2.获取连接（用户名  密码  端口号）


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }
}
